<template>
  <ParameterSettingsView v-if="loaded" :api-control="apiControl" />
</template>

<script setup lang="ts">
import type { ApplianceSetting } from '@/api/appliance'
import type { WaterHardType } from '@/api/common'
import { loadFileModule } from '@/controller/demo/utils'
import type { DemoDatasource, DatasourceMap } from '@/demo/types'
import { onLoad } from '@dcloudio/uni-app'
import ParameterSettingsView, { type ApiControl } from '../../settings/parameterSettingsView.vue'
import { mock } from '../utils'

let demoDatasource: DemoDatasource | null = null

const apiControl: ApiControl = {
  getSetting: mock((_, key) => {
    const settings = demoDatasource?.detailSettings || {}
    const setting = settings[key] as ApplianceSetting
    return setting
  }),
  setHomeApplianceSettings: mock(),
  setSetting: mock(),
  getWaterHard: () => Promise.resolve({} as WaterHardType),
}

const loaded = ref(false)
onLoad(async (options) => {
  const { DemoDatasourceMap } = await loadFileModule<{ DemoDatasourceMap: DatasourceMap }>('/demo/map')
  const type = options?.type || ''
  demoDatasource = DemoDatasourceMap[type]
  loaded.value = true
})
</script>
